﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace Metier
{
    public class GestionCarte
    {
        public void Ajouter(Carte carte)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Cartes.AddObject(carte);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public List<Carte> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Cartes.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Carte> Lister(Specialite specialite)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from p in db.Cartes
                            where p.Specialite.Id == specialite.Id
                            select p).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Carte Chercher(int carteId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Cartes.Where(c => c.Id == carteId).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Carte carte)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Cartes.Attach(carte);
                    db.ObjectStateManager.ChangeObjectState(carte, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Carte carte)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Cartes.DeleteObject(carte);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
